<%@page import="com.carlyang.studentinfo.util.LogUtil"%>
<%@page import="com.carlyang.studentinfo.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息管理</title>
<%
	String TAG = "classInfoManage.jsp";
	//权限验证，页面级的验证，如果功能页面多个，每个页面都得添加这段代码
	//使用session判断,session会话存放在服务器，只要用户不关闭浏览器，一直存在
	//LogUtil.i(TAG,session.getAttribute("currentUser").toString());
	User user = (User)session.getAttribute("currentUser");
	if(session.getAttribute("currentUser")==null ){
		response.sendRedirect("index.jsp");
		//System.out.println("服务器没有你登录成功的信息，滚回去登录！");
		LogUtil.i(TAG, "服务器没有你登录成功的信息，滚回去登录！");
		return;
	}
%>
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

	var URL;
	function searchStudent() {
		$('#studentInfo').datagrid('load',{
			stuNo:$('#s_stuNo').val(),//根据ID查询DOM节点后取值
			stuName:$('#s_stuName').val(),
			sex:$('#s_sex').combobox("getValue"),
			bbirthday:$('#s_bbirthday').datebox("getValue"),
			ebirthday:$('#s_ebirthday').datebox("getValue"),
			classId:$('#s_className').combobox("getValue")
		});
	}
	
	function deleteStudent() {
		var selectedRows = $('#studentInfo').datagrid('getSelections');
		if(selectedRows.length == 0){
			$.messager.alert("系统提示","您未选择数据，请选择要删除的数据！");
			return;
		}
		var strIds=[];
		for(var i=0;i<selectedRows.length;i++){
			strIds.push(selectedRows[i].id_stu);
		}
		var ids = strIds.join(",");
		$.messager.confirm("系统提示", "您确定要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
			if(r){
				$.post("studentDelete",{delIds:ids},function(result){//提交带JSON数据的Ajax请求到classDelete的servlet
					if(result.success){
						$.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNums+"</font>条数据！");
						$("#studentInfo").datagrid("reload");
					}else{
						$.messager.alert("系统提示","<font color=red>"+result.errorMsg+"</font>");
					}
				},"json");
			}
		})
	}
	
	
	/*
	注意：
		combobox用getValue获取的值是id，使用getText获取的是name
		在打开的学生信息对话框中，如果是add，因为是用户手动选择了班级信息，所以combobox用getValue获取的值是id
		但是如果是修改信息，打开对话框使用的是自动加载,使用getValue获取的是name而非id。
		解决办法：
		1.讲className改成classId，这个解决方法很奇怪
		2.在打开对话框后，需要选择一下班级的下拉，既可以使用getValue获取到id
		3.在打开对话框后，使用JQuery代码，取选中行中的classId信息来设置班级的combobox的值，用setValue或者select方法
	*/
	//打开学生信息添加对话框
	function openStudentAddDialog() {
		$("#dlg").dialog("open").dialog("setTitle","添加学生信息");
		URL="studentSave";
	}
	//打开学生信息修改对话框
	function openStudentModifyDialog() {
		//获取用户选中的信息行
		var selectedRows = $('#studentInfo').datagrid('getSelections');
		if(selectedRows.length != 1){
			$.messager.alert("系统提示","请选择一条要修改编辑的数据！");
			return;
		}
		var row = selectedRows[0];
		$("#dlg").dialog("open").dialog("setTitle","编辑修改班级信息");
		$("#fm").form("load",row);
		$('#className').combobox("setValue",row.classId);
		//alert($('#className').combobox("getValue"));
		URL="studentSave?id_stu="+row.id_stu;
	}
	//保存添加信息，调用后台
	function saveStudent(){
		$("#fm").form("submit",{
			url:URL,
			onSubmit:function(){
				if($('#sex').combobox("getValue")==""){
					$.messager.alert("系统提示","请选择性别！");
					return false;
				}
				if($('#className').combobox("getValue")==""){
					$.messager.alert("系统提示","请选择班级名称！");
					return false;
				}
				//alert($('#className').combobox("getValue"));
				return $(this).form("validate");
			},
			success:function(result){
				if(result.errorMsg){
					$.messager.alert("系统提示",result.errorMsg);
					return;
				}else{
					$.messager.alert("系统提示", "保存成功");
					resetStudentAddDialogValue();
					$("#dlg").dialog("close");
					$("#studentInfo").datagrid("reload");
				}
			}
		});
		
	}
	//关闭学生信息添加对话框
	function closeStudentAddDialog() {
		$("#dlg").dialog("close");
		resetStudentAddDialogValue();
	}
	//删除学生信息添加对话框中的内容
	function resetStudentAddDialogValue() {
		$("#stuNo").val("");
		$("#stuName").val("");
		$('#sex').combobox("setValue","");
		$('#birthday').datebox("setValue","");
		$('#className').combobox("setValue","");
		$("#email").val("");
		$("#stuDesc").val("");
	}
</script>
</head>
<body style="margin: 5px">
	<table id="studentInfo" title="学生信息" class="easyui-datagrid" 
		fitColumns="true" pagination="true" rownumbers="true" fit="true" url="studentList" toolbar="#tb"
		pageSize="20" pageList="[10,20,30,40,50]" nowrap="true" striped="true" collapsible="true"
		loadMsg="数据装载中......请稍等！">
		<thead>
			<tr>
				<th field="cb" checkbox="true"></th>
				<th field="id_stu" width="30" align="center">ID</th>
				<th field="stuNo" width="60" align="center">学号</th>
				<th field="stuName" width="60" align="center">姓名</th>
				<th field="sex" width="50" align="center">性别</th>
				<th field="birthday" width="60" align="center">出生日期</th>
				<th field="className" width="50" align="center">班级名称</th>
				<!-- <th field="classId" width="20" align="center" hidden="true">班级ID</th> -->
				<th field="email" width="90" align="center">邮箱</th>
				<th field="stuDesc" width="300" align="center">学生描述</th>
			</tr>
		</thead>
	</table>
	<div id="tb">
		<div>
			<a href="javascript:openStudentAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
			<a href="javascript:openStudentModifyDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">修改</a>
			<a href="javascript:deleteStudent()" class="easyui-linkbutton" iconCls="icon-add" plain="true">删除</a>
		</div>
		<div>
			&nbsp;学号：&nbsp;<input type="text" name="sc_stuNo" id="s_stuNo" size="15">
			&nbsp;姓名：&nbsp;<input type="text" name="sc_stuName" id="s_stuName" size="8">
			&nbsp;性别：&nbsp;<select class="easyui-combobox" id="s_sex" name="s_sex" editable="false" panelHeight="auto">
					<option value="">请选择性别</option>
					<option value="男">男</option>
					<option value="女">女</option>
					<option value="中性">中性</option>
				</select>
			&nbsp;生日：&nbsp;<input class="easyui-datebox" name="s_bbirthday" id="s_bbirthday" editable="false" size="15">--><input class="easyui-datebox" name="s_ebirthday" id="s_ebirthday" editable="false" size="15">
			<!-- data-options 值域不可编辑，页面显示用textField:'className'，选中时获取到的是 valueField:'id',页面加载时异步获取的请求是url:'classComboList'，后台会查询所有班级并返回JSON数据-->
			&nbsp;所属班级：&nbsp;<input class="easyui-combobox" id="s_className" name="s_className" size="10" panelHeight="auto" data-options="editable:false,valueField:'id',textField:'className',url:'classComboList'"/>
			<a href="javascript:searchStudent()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>

	<div id="dlg" class="easyui-dialog" style="width: 550px;height: 350px;padding: 10px 10px"
		closed="true" buttons="#dlg-buttons">
		<form id="fm" method="post">
			<table cellpadding="5px">			
				<tr>
					<td>学号：</td>
					<td><input type="text" name="stuNo" id="stuNo" class="easyui-validatebox" required="true"/></td>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
					<td>姓名：</td>
					<td><input type="text" name="stuName" id="stuName" class="easyui-validatebox" required="true"/></td>
				</tr>
				<tr>
					<td>性别：</td>
					<td><select class="easyui-combobox" id="sex" name="sex" editable="false" panelHeight="auto" required="true" style="width: 150px">
							<option value="">请选择性别</option>
							<option value="男">男</option>
							<option value="女">女</option>
							<option value="中性">中性</option>
						</select>
					</td>
					<td></td>
					<td>生日：</td>
					<td>
						&nbsp;<input class="easyui-datebox" name="birthday" id="birthday" editable="false" required="true">
					</td>
				</tr>
				<tr>
					<td>班级ID：</td>
					<td>
						&nbsp;<input class="easyui-combobox" id="className" name="className" panelHeight="auto" required="true" data-options="editable:false,valueField:'id',textField:'className',url:'classComboList'"/>
					</td>
					<td></td>
					<td>Email：</td>
					<td><input type="text" name="email" id="email" class="easyui-validatebox" required="true" validType="email"/></td>
				</tr>
				<tr>
					<td valign="top">班级描述：</td>
					<td colspan="4"><textarea rows="7" cols="45" type="text" name="stuDesc" id="stuDesc"></textarea></td>
				</tr>
			</table>
		</form>	
	</div>

	<div id="dlg-buttons">
		<a href="javascript:saveStudent()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">确定(OK)</a>
		<a href="javascript:closeStudentAddDialog()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">取消/关闭(Cancel)</a>
	</div>
</body>
</html>